﻿param(
    [string]$BuildDir = "build",
    [switch]$Clean
)

$ErrorActionPreference = "Stop"

if ($Clean -and (Test-Path $BuildDir)) {
    Remove-Item -Recurse -Force $BuildDir
}

if (-not (Test-Path $BuildDir)) {
    New-Item -ItemType Directory -Path $BuildDir | Out-Null
}

$cmakeArgs = @('-S', '.', '-B', $BuildDir)
if ($env:CMAKE_GENERATOR) {
    $cmakeArgs += @('-G', $env:CMAKE_GENERATOR)
}

cmake @cmakeArgs
cmake --build $BuildDir --config Debug
ctest --test-dir $BuildDir --output-on-failure --build-config Debug
